package com.kang.controller;

import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kang.pojo.AjaxResult;
import com.kang.pojo.Category;
import com.kang.service.CategoryService;
import com.vonechina.entity.page.CategoryPage;

/**
 * 物品类别控制层
 * 
 * @author zs614
 *
 */
@Controller
@RequestMapping("category")
@SessionAttributes({ "categoryPage" })
public class CategoryController extends com.kang.controller.Controller {
	@Resource
	private CategoryService cs;

	/**
	 * 查看物品分类列表界面
	 * 
	 * @param session
	 * @param model
	 * @param categoryPage
	 *            分页查询参数，封装为实体类
	 * @return 返回jsp页面
	 */
	@RequestMapping("listCategory")
	public String listCategory(HttpSession session, Model model, CategoryPage categoryPage) {
		initRight(session);
		if (checkRight("查看物品分类") != SUCCESS) {
			return "basic/noRight";
		}
		Category category = cs.selectByPrimaryKey(categoryPage.getCategoryid());
		if (category != null) {
			categoryPage.setFirstid(category.getFirstid());
			categoryPage.setSecondid(category.getSecondid() + 1);
		} else {
			categoryPage.setFirstid(0);
			categoryPage.setSecondid(1);
		}
		model.addAttribute("isAdd", checkRight("新增物品分类") == SUCCESS);
		model.addAttribute("isEdit", checkRight("编辑物品分类") == SUCCESS);
		model.addAttribute("isDelete", checkRight("删除物品分类") == SUCCESS);
		model.addAttribute("catagoryLilst", cs.listCategory(categoryPage));
		categoryPage.setPageUrl("category/listCategory.do");
		model.addAttribute("page", categoryPage);
		return "basicData/goodsType/listGoodsType";
	}

	/**
	 * 通过物品分类id查询物品分类 ajax接口
	 * 
	 * @param session
	 * @param categoryid
	 *            物品分类ID
	 * @return 返回物品分类实体类转json字符串
	 */
	@RequestMapping("findCategoryById")
	@ResponseBody
	public String findCategoryById(Integer categoryid) {
		Category category = cs.selectByPrimaryKey(categoryid);
		return JSONObject.toJSONString(category);
	}

	/**
	 * 编辑物品分类ajax接口
	 * 
	 * @param session
	 * @param category 物品分类自动封装实体类
	 * @return 返回json字符串
	 */
	@RequestMapping("update")
	@ResponseBody
	public String updatecategory(HttpSession session, Category category) {
		initRight(session);
		AjaxResult result = new AjaxResult();
		if (checkRight("编辑物品分类") != SUCCESS) {
			result.setResult("noRight");
			return JSON.toJSONString(result);
		}
		int a = cs.updateByPrimaryKeySelective(category);
		if (a > 0) {
			result.setResult("success");
		}
		return JSONObject.toJSONString(result);
	}

	/**
	 * 删除物品分类ajax接口
	 * 
	 * @param session
	 * @param categoryid 物品分类id
	 * @return 返回json字符串
	 */
	@RequestMapping("delete")
	@ResponseBody
	public String deletecategory(HttpSession session, Integer categoryid) {
		initRight(session);
		AjaxResult result = new AjaxResult();
		if (checkRight("删除物品分类") != SUCCESS) {
			result.setResult("noRight");
			return JSON.toJSONString(result);
		}
		Category category = cs.selectByPrimaryKey(categoryid);
		if (category.getSecondid() == 1) {
			List<Category> list = cs.selectid(categoryid);
			for (Category cate : list) {
				cs.deletenext(cate.getCategoryid());
			}
			cs.deletenext(categoryid);
		} else if (category.getSecondid() == 2) {
			cs.deletenext(categoryid);
		}
		int a = cs.deleteByPrimaryKey(categoryid);
		if (a > 0) {
			result.setResult("success");
		}
		return JSONObject.toJSONString(result);
	}

	/**
	 * 添加物品分类ajax接口
	 * 
	 * @param session
	 * @param category 物品分类自动封装实体类
	 * @return
	 */
	@RequestMapping("add")
	@ResponseBody
	public String addcategory(HttpSession session, Category category) {
		initRight(session);
		AjaxResult result = new AjaxResult();
		if (checkRight("新增物品分类") != SUCCESS) {
			result.setResult("noRight");
			return JSON.toJSONString(result);
		}
		if (category.getFirstid() != 0) {
			Category cg = cs.selectByPrimaryKey(category.getFirstid());
			category.setSecondid(cg.getSecondid() + 1);
		} else {
			category.setSecondid(1);
		}
		if (category.getSecondid() > 3) {
			result.setError("无法新增三级以上子分类");
			return JSONObject.toJSONString(result);
		}
		category.setCategorycode(cs.generateCategoryCode());
		category.setCreatetime(new Date());
		category.setCreatepersonid((Integer) session.getAttribute("userID"));
		int a = cs.insertSelective(category);
		if (a > 0) {
			result.setResult("success");
		}
		return JSONObject.toJSONString(result);
	}
}
